#!/bin/bash
#
# reader      This shell script takes care of starting and stopping Sismics Reader
#
# chkconfig: - 80 20
#
### BEGIN INIT INFO
# Provides:          reader
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Sismics Reader daemon
# Description:       Starts the Sismics Reader daemon. Reader is a Web-based
#                    aggregator of content served by Web Feeds.
#                    See http://github.com/sismics/reader for more details.
### END INIT INFO

# Author: Jean-Marc Tremeaux <jm.tremeaux@gmail.com>

# To change the startup parameters of Reader, modify the service
# configuration file /etc/sysconfig/reader rather than this file.
[ -r /etc/sysconfig/reader ] && . /etc/sysconfig/reader

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Reader Daemon"
NAME=reader
PIDFILE=/var/run/$NAME.pid
LOCKFILE=/var/lock/subsys/$NAME
DAEMON=/usr/bin/$NAME
DAEMON_ARGS="--pidfile=$PIDFILE $READER_ARGS"
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Run as root if READER_USER is not set.
[ "$READER_USER" = "" ] && READER_USER=root

# Source function library.
. /etc/init.d/functions

#
# Function that starts the daemon/service
#
do_start()
{
    # Check if daemon is already running.
    if [ -e $PIDFILE ]
    then
	ps -p $(cat $PIDFILE) > /dev/null
	[ "$?" = 0 ] && return 1
    fi

    touch $PIDFILE
    chown $READER_USER $PIDFILE
    [ -e /var/reader ] && chown -R $READER_USER /var/reader

    echo $"Starting $NAME ..."
    su -c "$DAEMON $DAEMON_ARGS" $READER_USER
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch $LOCKFILE
    return $RETVAL
}

#
# Function that stops the daemon/service
#
do_stop()
{
    # Check if pidfile exists
    [ ! -e $PIDFILE ] && return 1

    echo -n $"Stopping $NAME ..."
    killproc -p $PIDFILE $DAEMON
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f $LOCKFILE
    return $RETVAL
}

case "$1" in
  start)
	do_start
	;;
  stop)
	do_stop
	;;
  status)
       status -p $PIDFILE "$NAME"
       ;;
  restart|force-reload)
	do_stop
	do_start
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
	exit 3
	;;
esac

:
